package com.JB;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

public class pictureFrame extends JFrame {

    //定义二维数组存储图片编号
    private int[][] dates = {
            {1, 2, 3, 4},
            {5, 6, 7, 8},
            {9, 10, 11, 12},
            {13, 14, 15, 0}};

    private int[][] windates = {
            {1, 2, 3, 4},
            {5, 6, 7, 8},
            {9, 10, 11, 12},
            {13, 14, 15, 0}};

    //0号图片索引
    private int x0;
    private int y0;

    private JButton shangbutton;
    private JButton xiabutton;
    private JButton zuobutton;
    private JButton youbutton;
    private JButton qiuzhubutton;
    private JButton chongzhibutton;

    private JPanel imagePanel;

    public pictureFrame() {
        initFrame();
        randomDates();
        paintView();
        addButtonEvent();
        setVisible(true);
    }

    public void randomDates() {
        Random r = new Random();
        for (int i = 0; i < dates.length; i++) {
            for (int j = 0; j < dates[i].length; j++) {
                int x = r.nextInt(dates.length);
                int y = r.nextInt(dates[i].length);
                int temp = dates[i][j];
                dates[i][j] = dates[x][y];
                dates[x][y] = temp;
            }
        }
       wc: for (int i = 0; i < dates.length; i++) {
            for (int j = 0; j < dates[i].length; j++) {
            if(dates[i][j]==0){
                x0=i;
                y0=j;
                break wc;//带标签的循环，结束多层循环
            }
            }
        }
    }

    public void initFrame() {
        this.setTitle("动漫拼图");
        this.setSize(960, 565);
        this.setDefaultCloseOperation(3);
        this.setLocationRelativeTo(null);
        this.setAlwaysOnTop(true);
        this.setLayout(null);
    }

    public void paintView() {
        //标题图片
        JLabel titlelable = new JLabel(new ImageIcon("puzzlesGame\\images\\title.png"));
        titlelable.setBounds(354, 27, 232, 57);
        this.add(titlelable);

        //创建面板
        imagePanel = new JPanel();
        imagePanel.setBounds(150, 114, 360, 360);
        imagePanel.setLayout(null);
        this.add(imagePanel);

        //遍历二维数组得到图片编号
        for (int i = 0; i < dates.length; i++) {
            for (int j = 0; j < dates[i].length; j++) {
                JLabel imagelable = new JLabel(new ImageIcon("puzzlesGame\\images\\" + dates[i][j] + ".png"));
                imagelable.setBounds(j * 90, i * 90, 90, 90);
                imagePanel.add(imagelable);
            }
        }
        //动漫参照图
        JLabel canzhaotulable = new JLabel(new ImageIcon("puzzlesGame\\images\\canzhaotu.png"));
        canzhaotulable.setBounds(574, 114, 122, 121);
        this.add(canzhaotulable);

        //按钮
         shangbutton = new JButton(new ImageIcon("puzzlesGame\\images\\shang.png"));
         xiabutton = new JButton(new ImageIcon("puzzlesGame\\images\\xia.png"));
         zuobutton = new JButton(new ImageIcon("puzzlesGame\\images\\zuo.png"));
         youbutton = new JButton(new ImageIcon("puzzlesGame\\images\\you.png"));
         qiuzhubutton = new JButton(new ImageIcon("puzzlesGame\\images\\qiuzhu.png"));
        chongzhibutton = new JButton(new ImageIcon("puzzlesGame\\images\\chongzhi.png"));
        shangbutton.setBounds(732, 265, 57, 57);
        xiabutton.setBounds(732, 347, 57, 57);
        zuobutton.setBounds(650, 347, 57, 57);
        youbutton.setBounds(813, 347, 57, 57);
        qiuzhubutton.setBounds(626, 444, 108, 45);
        chongzhibutton.setBounds(786, 444, 108, 45);
        this.add(shangbutton);
        this.add(xiabutton);
        this.add(zuobutton);
        this.add(youbutton);
        this.add(qiuzhubutton);
        this.add(chongzhibutton);

        //背景图
        JLabel backgroundlable = new JLabel(new ImageIcon("puzzlesGame\\images\\background.png"));
        backgroundlable.setBounds(0, 0, 960, 530);
        this.add(backgroundlable);


    }

    public void addButtonEvent(){
        shangbutton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
            if(x0==3) return;
            else{
                dates[x0][y0]=dates[x0+1][y0];
                dates[x0+1][y0]=0;
                x0+=1;
            }
            if(isSuccess()){
                success();
            }
            repaintView();
            }
        });
        xiabutton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
            if(x0==0) return;
            else{
                dates[x0][y0]=dates[x0-1][y0];
                dates[x0-1][y0]=0;
                x0-=1;
            }
                if(isSuccess()){
                    success();
                }
            repaintView();
            }
        });
        zuobutton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(y0==3) return;
                else {
                    dates[x0][y0]=dates[x0][y0+1];
                    dates[x0][y0+1]=0;
                    y0+=1;
                }
                if(isSuccess()){
                    success();
                }
                repaintView();
            }
        });
        youbutton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
            if(y0==0) return;
            else{
                dates[x0][y0]=dates[x0][y0-1];
                dates[x0][y0-1]=0;
                y0-=1;
            }
                if(isSuccess()){
                    success();
                }
            repaintView();
            }
        });
        qiuzhubutton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
            success();
            repaintView();
            }
        });
        chongzhibutton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dates=new int[][]{
                        {1, 2, 3, 4},
                        {5, 6, 7, 8},
                        {9, 10, 11, 12},
                        {13, 14, 15, 0}};
            randomDates();
            repaintView();
            shangbutton.setEnabled(true);
            xiabutton.setEnabled(true);
            zuobutton.setEnabled(true);
            youbutton.setEnabled(true);

            }
        });
    }

    public void repaintView(){
       imagePanel.removeAll();
        imagePanel.repaint();
        for (int i = 0; i < dates.length; i++) {
            for (int j = 0; j < dates[i].length; j++) {
                JLabel imagelable = new JLabel(new ImageIcon("puzzlesGame\\images\\" + dates[i][j] + ".png"));
                imagelable.setBounds(j * 90, i * 90, 90, 90);
                imagePanel.add(imagelable);
            }
        }


    }

    public void success(){
        dates = new int[][]{
                {1, 2, 3, 4},
                {5, 6, 7, 8},
                {9, 10, 11, 12},
                {13, 14, 15, 16}};

        shangbutton.setEnabled(false);
        xiabutton.setEnabled(false);
        zuobutton.setEnabled(false);
        youbutton.setEnabled(false);

    }

    public boolean isSuccess() {
        for (int i = 0; i < dates.length; i++) {
            for (int j = 0; j < dates[i].length; j++) {
                if (dates[i][j] != windates[i][j])
                    return false;
            }
        }
        return true;
    }

}
